package cn.com.bluemoon.daps.common.datascope.encry;

import cn.com.bluemoon.metadata.common.ResultBean;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Slf4j
@Component
public class DataEncryptAspect {
 
    /**
     * 开关，控制是否开启接口返回加密
     */
    @Value("${dataEncrypt.resp.enableEncrypt:true}")
    private boolean enableEncrypt;

    @Autowired
    private AesWapper aesWapper;

 

    @Around("@annotation(cn.com.bluemoon.daps.common.datascope.DataEncrypt)")
    public Object AspectByExecution(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Object obj = joinPoint.proceed();
        return dataEncrypt(obj, method);
    }
 
    /**
     * 对对象为ResponseBean进行数据加密
     * @param object
     * @return
     */
    private Object dataEncrypt(Object object, Method method) {
        if(!this.enableEncrypt || aesWapper.isEnableDecrypt()) {
            return object;
        }
        try {
            if(object instanceof ResultBean) {
                ResultBean responseEncryptBean = new ResultBean();
                BeanUtil.copyProperties(object, responseEncryptBean);
                if(responseEncryptBean.isSuccess()) {
                    //响应成功且data有值才进行加密
                    Object data = responseEncryptBean.getContent();
                    if(data != null) {
                        String dataStr = JSONObject.toJSONString(data);
                        if(!StrUtil.isBlank(dataStr)) {
                            //对data进行aes对称加密
                            responseEncryptBean.setEncrypt(true);
                            responseEncryptBean.setContent(aesWapper.encryptBase64(dataStr));
                        }
                    }
                }
                return responseEncryptBean;
            }else if (object instanceof cn.com.bluemoon.daps.common.domain.ResultBean) {
                cn.com.bluemoon.daps.common.domain.ResultBean resultBean = (cn.com.bluemoon.daps.common.domain.ResultBean) object;
                if(resultBean.isOk()) {
                    //响应成功且data有值才进行加密
                    Object data = resultBean.getContent();
                    if(data != null) {
                        String dataStr = JSONObject.toJSONString(data);
                        if(!StrUtil.isBlank(dataStr)) {
                            //对data进行aes对称加密
                            resultBean.setContent(aesWapper.encryptBase64(dataStr));
                        }
                    }
                }
                return resultBean;
            }
        }catch (Exception e) {
            log.error("ResponseBean进行数据加密发生错误，方法{}", method.getName(), e);
        }
        return object;
    }
 

}